package com.solution.service;

import java.util.Iterator;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;

import com.solution.constant.ErrorMessages;
import com.solution.constant.ExamErrorMessages;
import com.solution.constant.StatusConstants;
import com.solution.dao.CategoryTypeDAO;
import com.solution.dao.DAOFactory;
import com.solution.exception.DAOException;
import com.solution.exception.ServiceBeanException;
import com.solution.model.CategoryType;
import com.solution.util.SearchVO;

@Stateless
@Remote(CategoryTypeService.class)
@RemoteBinding(jndiBinding="CategoryTypeService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CategoryTypeServiceImpl implements CategoryTypeService{

	/**
	 * DAO instance.
	 */
	private CategoryTypeDAO categoryTypeDao = (CategoryTypeDAO)DAOFactory.getDAOInstance(CategoryTypeDAO.class.getName());
	
	/**
	 * Logger instance.
	 */
	private Logger log = Logger.getLogger(CategoryTypeServiceImpl.class.getName());

	public List<CategoryType> fetchActiveCategories(Long clientId) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:fetchActiveCategories()");
		try{
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
			searchVO.addEqualsCondition("clientType.id", clientId, true);
			return categoryTypeDao.search(searchVO);
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public List<CategoryType> getCategories(SearchVO searchVO) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:getCategories()");
		try{
			return categoryTypeDao.search(searchVO);
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public CategoryType getCategory(Long categoryTypeId) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:getCategory()");
		try{
			if(categoryTypeId != null)
				return categoryTypeDao.read(categoryTypeId);
			else
				throw new ServiceBeanException(ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION+" categoryTypeId");
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public CategoryType createCategory(CategoryType categoryType) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:createCategory()");
		try{
			// Perform business validations.
			// check for the mandatory parameters.
			if(categoryType.checkForMandatoryParameters() != null && categoryType.checkForMandatoryParameters().size() > 0){
				String errorMsg = ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION;
				Iterator<String> iter = categoryType.checkForMandatoryParameters().iterator();
				while(iter.hasNext()){
					errorMsg = errorMsg+", "+iter.next();
				}
				throw new ServiceBeanException(errorMsg);
			}
			else{
				if(categoryTypeDao.verifyUnique("name", categoryType.getName(), null)){
					// Create the answer in database.
					categoryType = categoryTypeDao.create(categoryType);
					log.info("The new category created with ID : "+categoryType.getId().longValue());
					log.debug("Exiting :: CategoryTypeServiceImpl:createCategory()");
					return categoryType;
				}
				else{
					throw new ServiceBeanException(ExamErrorMessages.CATEGORY_DUPLICATE_NAME_EXCEPTION);
				}
			}
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_CREATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public CategoryType modifyCategory(CategoryType categoryType) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:modifyCategory()");
		try{
			// Perform business validations.
			// check for the mandatory parameters.
			if(categoryType.checkForMandatoryParameters() != null && categoryType.checkForMandatoryParameters().size() > 0){
				String errorMsg = ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION;
				Iterator<String> iter = categoryType.checkForMandatoryParameters().iterator();
				while(iter.hasNext()){
					errorMsg = errorMsg+", "+iter.next();
				}
				throw new ServiceBeanException(errorMsg);
			}
			else{
				if(categoryTypeDao.verifyUnique("name", categoryType.getName(), categoryType.getId())){
					// Create the answer in database.
					categoryType = categoryTypeDao.update(categoryType);
					log.info("The category updated with ID : "+categoryType.getId().longValue());
					log.debug("Exiting :: CategoryTypeServiceImpl:modifyCategory()");
					return categoryType;
				}
				else{
					throw new ServiceBeanException(ExamErrorMessages.CATEGORY_DUPLICATE_NAME_EXCEPTION);
				}
			}
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_MODIFY_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
}
